package com.hmyuan.controller.back;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hmyuan.domain.SysDictionaryData;
import com.hmyuan.domain.SysDictionaryType;
import com.hmyuan.service.SysDictionaryDataService;
import com.hmyuan.service.SysDictionaryTypeService;

/**
 * 字典controller
 * @author wbtianhl
 *
 */
@Controller
@RequestMapping("/back/dictionary")
public class DictionaryController {

	@Resource
	SysDictionaryTypeService sysDictionaryTypeService;
	@Resource
	SysDictionaryDataService sysDictionaryDataService;
	
	/**
	 *字典类型查询页面 
	 */
	@RequestMapping("dictionaryTypePage")
	public String dictionaryTypePage(HttpServletRequest request,Model model){
		
		return "/back/dictionary/dictionaryTypePage";
	}
	/**
	 *分页查询字典类型 
	 */
	@RequestMapping("dictionaryTypeList")
	public String dictionaryTypeList(HttpServletRequest request,Model model,Integer pageNum,SysDictionaryType sysDictionaryType){
		
		PageInfo<SysDictionaryType> pageInfo = sysDictionaryTypeService.selectListByPage(sysDictionaryType,pageNum,10);
		
		request.setAttribute("pageInfo", pageInfo);
		
		return "/back/dictionary/dictionaryTypeList";
	}
	/**
	 *添加字典类型页面 
	 */
	@RequestMapping("dictionaryTypeAddPage")
	public String dictionaryTypeAddPage(HttpServletRequest request,Model model){
		
		return "/back/dictionary/dictionaryTypeAddPage";
	}
	/**
	 *添加字典类型提交 
	 */
	@ResponseBody
	@RequestMapping("adddictionaryType")
	public String adddictionaryType(HttpServletRequest request,Model model,SysDictionaryType sysDictionaryType){
		
		sysDictionaryTypeService.insert(sysDictionaryType);
		
		return "success";
	}
	/**
	 *更新字典类型页面 
	 */
	@RequestMapping("dictionaryTypeUpdatePage")
	public String dictionaryTypeUpdatePage(HttpServletRequest request,Model model,Integer id){
		if(null==id) return "/back/dictionary/dictionaryTypeAddPage";

		request.setAttribute("dictionaryType", sysDictionaryTypeService.selectByPrimaryKey(id));
		
		return "/back/dictionary/dictionaryTypeUpdatePage";
	}
	/**
	 *更新字典类型提交 
	 */
	@ResponseBody
	@RequestMapping("updatedictionaryType")
	public String updatedictionaryType(HttpServletRequest request,Model model,SysDictionaryType sysDictionaryType){
		
		if(null==sysDictionaryType.getId()) return "fail";
		
		sysDictionaryTypeService.updateByPrimaryKeySelective(sysDictionaryType);
		
		return "success";
	}
	/**
	 * 检查类型编码是否可用
	 */
	@ResponseBody
	@RequestMapping("/checkdictionaryTypeCode")
	public String checkdictionaryTypeCode(HttpServletRequest request,Model model,String typecode,Integer id){
		if(null==typecode||"".equals(typecode) ) return "fail";
		SysDictionaryType sysDictionaryType = new SysDictionaryType();
		sysDictionaryType.setTypecode(typecode);
		List<SysDictionaryType> list = sysDictionaryTypeService.selectList(sysDictionaryType);
		if(null==list||list.size()==0||(id!=null&&list.size()==1&&list.get(0).getId()==id)){
			return "success";
		}else{
			return "fail";
		}
	}
	/**
	 *删除字典类型提
	 */
	@ResponseBody
	@RequestMapping("deletedictionaryTypeById")
	public String deletedictionaryTypeById(HttpServletRequest request,Model model,Integer id){
		
		if(null==id) return "fail";
		
		sysDictionaryTypeService.deleteByPrimaryKey(id);
		
		return "success";
	}
	//-----------------------------------------------------------------------------------------
	/**
	 *字典数据查询页面 
	 */
	@RequestMapping("dictionaryDataPage")
	public String dictionaryDataPage(HttpServletRequest request,Model model,Integer dictionaryTypeId){
		
		request.setAttribute("dictionaryTypeId", dictionaryTypeId);
		
		return "/back/dictionary/dictionaryDataPage";
	}
	/**
	 *分页查询字典数据 
	 */
	@RequestMapping("dictionaryDataList")
	public String dictionaryDataList(HttpServletRequest request,Model model,Integer pageNum,SysDictionaryData sysDictionaryData){
		
		PageInfo<SysDictionaryData> pageInfo = sysDictionaryDataService.selectListByPage(sysDictionaryData,pageNum,10);
		
		request.setAttribute("pageInfo", pageInfo);
		
		return "/back/dictionary/dictionaryDataList";
	}
	/**
	 *添加字典数据页面 
	 */
	@RequestMapping("dictionaryDataAddPage")
	public String dictionaryDataAddPage(HttpServletRequest request,Model model,Integer dictionaryTypeId){
		
		request.setAttribute("dictionaryTypeId", dictionaryTypeId);
		
		return "/back/dictionary/dictionaryDataAddPage";
	}
	/**
	 *添加字典数据提交 
	 */
	@ResponseBody
	@RequestMapping("adddictionaryData")
	public String adddictionaryData(HttpServletRequest request,Model model,SysDictionaryData sysDictionaryData){
		
		sysDictionaryDataService.insert(sysDictionaryData);
		
		return "success";
	}
	/**
	 *更新字典数据页面 
	 */
	@RequestMapping("dictionaryDataUpdatePage")
	public String dictionaryDataUpdatePage(HttpServletRequest request,Model model,Integer id,Integer dictionaryTypeId){
		if(null==id) return "/back/dictionary/dictionaryDataAddPage";

		request.setAttribute("dictionaryData", sysDictionaryDataService.selectByPrimaryKey(id));
		request.setAttribute("dictionaryTypeId", dictionaryTypeId);

		return "/back/dictionary/dictionaryDataUpdatePage";
	}
	/**
	 *更新字典数据提交 
	 */
	@ResponseBody
	@RequestMapping("updatedictionaryData")
	public String updatedictionaryData(HttpServletRequest request,Model model,SysDictionaryData sysDictionaryData){
		
		if(null==sysDictionaryData.getId()) return "fail";
		
		sysDictionaryDataService.updateByPrimaryKeySelective(sysDictionaryData);
		
		return "success";
	}
	/**
	 * 检查数据编码是否可用
	 */
	@ResponseBody
	@RequestMapping("/checkdictionaryDataName")
	public String checkdictionaryDataName(HttpServletRequest request,Model model,String name,Integer dictionaryTypeId,Integer id){
		if(null==name||"".equals(name)||
				null==dictionaryTypeId||"".equals(dictionaryTypeId) ) return "fail";
		SysDictionaryData sysDictionaryData = new SysDictionaryData();
		sysDictionaryData.setName(name);
		sysDictionaryData.setDictionaryTypeId(dictionaryTypeId);
		List<SysDictionaryData> list = sysDictionaryDataService.selectList(sysDictionaryData);
		if(null==list||list.size()==0||(id!=null&&list.size()==1&&list.get(0).getId()==id)){
			return "success";
		}else{
			return "fail";
		}
	}
	/**
	 *删除字典数据提
	 */
	@ResponseBody
	@RequestMapping("deletedictionaryDataById")
	public String deletedictionaryDataById(HttpServletRequest request,Model model,Integer id){
		
		if(null==id) return "fail";
		
		sysDictionaryDataService.deleteByPrimaryKey(id);
		
		return "success";
	}
}
